// pages/home/shopMessage/shopMessage.js
const app =getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow: true,//商家简介是否显示
    btnArr: [
      { 'tit': '联系客服', 'img': '../../../images/others/connect.png'},
      { 'tit': '收藏店铺', 'img': '../../../images/others/collect.png' },
      { 'tit': '去店铺', 'img': '../../../images/others/navigation.png' }],
    sInfo:{},//商家详情
    pInfo:{},//产品信息
    aInfo:{},//动态信息
    cInfo:{},//优惠券信息

    imgURL: app.globalData.imgURL,//图片总路径
    isShare:'0',//是否是分享的 如果是 显示一个悬浮按钮
  },

  //营业执照点击
  licenceClick:function(){
    var that = this;
    var nArr = [that.data.imgURL + that.data.sInfo.license];
    console.log(nArr);

    wx.previewImage({
      current: nArr[0],
      urls: nArr,
    })
  },

  //跳转home页面
  homeClick:function(){
    console.log('返回按钮点击');
    wx.reLaunch({
      url: '../homeFace/homeFace',
    })
  },

  imgClick:function(e){
    var that = this;
    console.log(e.currentTarget.id);
    var imgArr = that.data.aInfo.img_url;
    var nImgArr = new Array();
    for (var i = 0; i < imgArr.length; i++) {
      nImgArr[i] = that.data.imgURL + imgArr[i].replace('_thum', '');
    }
    wx.previewImage({
      current: nImgArr[e.currentTarget.id],
      urls: nImgArr,
    })

  },

  phoneClick: function (e) {
    console.log(e.currentTarget.id);
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.id,
    })
  },


  shopCardClick:function(e){
    var that = this;
    
    console.log(e.currentTarget.id);
    if (e.currentTarget.id == 0){
      //客服
      wx.showToast({
        title: '暂无开通，敬请期待',
        icon:'none'
      })
    } else if (e.currentTarget.id == 1){
      //收藏
      if (!wx.getStorageSync('userInfo')) {
        wx.showToast({
          title: '请先登录',
          icon: 'none',
        })
      }else{
        app.post('collect/add', { openId: wx.getStorageSync('userInfo').userId, shopId: that.data.sInfo.id}).then((res) => {
          console.log('success');
          console.log(res);
          wx.showToast({
            title: res.msg,
            icon: 'none',
          })
        }).catch((errMsg) => {
          console.log('fail');
          console.log(errMsg + '失败');
        })
      }
    }else{
      //导航
      wx.openLocation({
        latitude: Number(that.data.sInfo.latitude),
        longitude: Number(that.data.sInfo.longitude),
      })
    }
  },

  //商家动态更多
  shopDynamicClick:function(){
    var that = this;

    if (!wx.getStorageSync('userInfo')) {
      wx.showToast({
        title: '请先登录',
        icon:'none',
      })
      return;
    }

    wx.navigateTo({
      url: '../shopDynamic/shopDynamic?id=' + that.data.sInfo.id,
    })

  },


  //点击产品进入详情
  productClick:function(e){
    console.log(e.currentTarget.id);
    wx.navigateTo({
      url: '../productDetail/productDetail?tit=' + e.currentTarget.dataset.tit + '&id=' + e.currentTarget.id + ' &openid=' + e.currentTarget.dataset.openid,
    })
  },

  //优惠券点击
  cardClick:function(e){
    console.log(e.currentTarget.id);
    wx.navigateTo({
      url: '../shopCheapDetail/shopCheapDetail?id=' +e.currentTarget.id,
    })
  },


  //商家简介是否显示
  showOrHidden: function () {
    var that = this;
    that.setData({
      isShow: !that.data.isShow,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    that.requestData(options.id);
    if (options.isShare){
      that.setData({
        isShare : options.isShare,
      })
    }

  },

  requestData: function (openId) {
    var that = this;
    app.post('shop/queryShopInfoForOther', { openId: openId}).then((res) => {
      console.log('success');
      console.log(res);
      if (res.code == 0) {
        var nProduces = res.produces;
        var nActiveInfo = res.activeInfo;

        if (typeof (nActiveInfo) != "undefined" ){
          if (typeof (nActiveInfo.img_url) != "undefined") {
            nActiveInfo.img_url = nActiveInfo.img_url.split(',');
          }
        }
        for (var i = 0; i < nProduces.length; i++) {
          nProduces[i].pro_img = nProduces[i].pro_img.split(',');
        }

        that.setData({
          sInfo: res.shopInfo,//商家详情
          pInfo: nProduces,//产品信息
          aInfo: nActiveInfo,//动态信息
          cInfo: res.coupons,//优惠券信息
        })
      } else {
        wx.showModal({
          title: '温馨提示',
          content: res.msg,
          showCancel: false,
        })
      }
    }).catch((errMsg) => {
      console.log('fail');
      console.log(errMsg + '失败');
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})